import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle,Ellipse

#第一题(两个数字加减乘除)
# q=input("输入公式:")
# if '+' in q:
#     w,e=float(q.split('+')[0]),float(q.split("+")[1])
#     q=w+e
# elif '-' in q:
#     w,e=float(q.split('-')[0]),float(q.split("-")[1])
#     q=w-e
# elif '*' in q:
#     w,e=float(q.split('*')[0]),float(q.split("*")[1])
#     q=w*e
# elif '/' in q:
#     w,e=float(q.split('/')[0]),float(q.split("/")[1])
#     q=w/e
#
# print('计算结果为{}'.format(q))



#第二题
# n = pow(2, 20)
# r = 1
# a,b = (0.,0.)
# xi, xa = a-r, a+r
# yi, ya = b-r, b+r
# x = np.random.uniform(xi, xa, n)
# y = np.random.uniform(yi, ya, n)
# d = np.sqrt(np.square(x-a) + np.square(y-b))
# res = sum(np.where(d < r, 1, 0))
# pi = 4 * res / n
# print('pi:',pi)
#
# fig = plt.figure()
# axes = fig.add_subplot(111)
# axes.plot(x, y,'ro',markersize = 0.01)
# plt.axis('equal')
# circle = Circle(xy=(a,b), radius=r)
# axes.add_patch(circle)
# plt.show()



#第三题





pic=plt.figure()
ax=pic.add_subplot(111)
ellipse=Ellipse(xy = (0.0, 0.0), width = 4, height = 8)
ax.add_patch(ellipse)
plt.axis('scaled')

n = pow(2, 20)
y=np.random.uniform(-4,4,n)
x=np.random.uniform(-2,2,n)
f=np.square(x)/4+np.square(y)/16

array1=np.where( np.all((f < 1) &  (x-2*y-2 <0) ), 1, 0)
array2=np.where(np.all( (f  < 1) &  (x-2*y-2 > 0)), 1, 0)

line_up = np.sum(array1, axis=0)
line_down = np.sum(array2, axis=0)

re=sum( np.where( f < 1, 1, 0 ) )
ax.plot(x, y,'ro',markersize = 0.01)

ellipse_area=32*re/n
line_up_area=32*line_up/n
line_down_area=32*line_down/n
print(ellipse_area,line_up_area,line_down_area)
print(ellipse_area)
pic.show()